/**
 * 
 */

package it.bd.SetAllConnectionsOff;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Bundle;
import android.widget.RemoteViews;
import android.widget.Toast;

import it.bd.SetAllConnectionsOff.helper.StateHelper;

/**
 * @author Mihai HARAS (Lohmar ASHAR) 2011
 */
public class WidgetActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        boolean limitedStateActive = StateHelper.getInstance().getState();
        Toast.makeText(this, limitedStateActive ? R.string.msgLimitedDeactivate : R.string.msgLimitedActivate, Toast.LENGTH_SHORT).show();
        StateHelper.getInstance().setState(!limitedStateActive);
        fixIcon(this);
        finish();
    }

    public static void fixIcon(Context context) {
        StateHelper sh = StateHelper.getInstance();
        if (sh == null) {
            sh = new StateHelper((ContextWrapper) context);
        }
        boolean state = sh.getState();
        if (state) {
            changeWidgetIcon(R.drawable.battery_icon, context);
        } else {
            changeWidgetIcon(R.drawable.icon, context);
        }
    }

    private static void changeWidgetIcon(int icon, Context context) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
        views.setImageViewResource(R.id.widgetimage, icon);
        ComponentName thisWidget = new ComponentName(context, SetAllConnectionsOffWidget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(thisWidget, views);
    }
}
